﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HammerMUD.System;
using HammerMUD.Races;
using HammerMUD.Professions;
using HammerMUD.Accounts;
using HammerMUD.Areas;
using HammerMUD.Rooms;

namespace HammerMUD.Characters
{
    public class NewCharacterUtilities
    {
        public static void ProcessNewCharacter(String line, User user, List<Race> races, List<Account> accounts)
        {
            switch (user.CurrentState.NewCharacter)
            {
                case NewCharacterState.Name:
                    Name(line, user, HammerMUD.System.HammerMUD.Users);
                    break;
                case NewCharacterState.Class:
                    Class(line, user, accounts);
                    break;
                case NewCharacterState.Race:
                    Race(line, user, races);
                    break;
                case NewCharacterState.Sex:
                    Sex(line, user, races);
                    break;
                default:
                    Console.WriteLine("Error! Invalid new character state");
                    break;
            }
        }

        public static void Class(String line, User user, List<Account> accounts)
        {
            foreach (Profession p in user.UserAccount.CurrentCharacter.Race.AllowedProfessions)
            {
                if (line.Trim().ToLower().Equals(p.Name.ToLower()))
                {
                    user.UserAccount.CurrentCharacter.CurrentRoom = HammerMUD.System.HammerMUD.EmptyRoom;
                    foreach (Area a in HammerMUD.System.HammerMUD.Areas)
                    {
                        Room temporaryRoom;
                        if ((temporaryRoom = a.FindRoom(HammerMUD.System.HammerMUD.SystemInfo.StartRoomNumber)) != null)
                        {
                            user.UserAccount.CurrentCharacter.CurrentRoom = temporaryRoom;
                            break;
                        }
                    }

                    user.UserAccount.CurrentCharacter.Account = user.UserAccount;
                    user.UserAccount.CurrentCharacter.CurrentRoom.Characters.Add(user.UserAccount.CurrentCharacter);
                    user.UserAccount.CurrentCharacter.Profession = p;
                    user.CurrentState.Base = BaseState.Command;
                    user.CurrentState.Command = CommandState.Command;
                    user.UserAccount.Characters.Add(user.UserAccount.CurrentCharacter);
                    SystemUtilities.SaveAccounts(accounts);
                    return;
                }
            }

            user.Send("Invalid profession!");
            foreach (Profession p in user.UserAccount.CurrentCharacter.Race.AllowedProfessions)
            {
                user.Send("\t" + p.Name);
            }

            user.Prompt("What class will " + user.UserAccount.CurrentCharacter.Name + " be? ");
        }

        public static void Race(String line, User user, List<Race> races)
        {
            foreach (Race r in races)
            {
                if (line.Trim().ToLower().Equals(r.Name.ToLower()))
                {
                    user.UserAccount.CurrentCharacter.Race = r;
                    user.CurrentState.NewCharacter = NewCharacterState.Class;
                    user.Send(user.UserAccount.CurrentCharacter.Name + " is " + r.Name);

                    foreach (Profession p in r.AllowedProfessions)
                    {
                        user.Send("\t" + p.Name);
                    }

                    user.Prompt("What profession will " + user.UserAccount.CurrentCharacter.Name + " be? ");
                    return;
                }
            }

            user.Send("Not a valid race! Select from the following races: ");
            foreach (Race r in races)
            {
                user.Send("\t" + r.Name);
            }
        }

        public static void Name(String line, User user, List<User> users)
        {
            user.UserAccount.CurrentCharacter = new Character();

            foreach (User u in users)
            {
                foreach (Character c in u.UserAccount.Characters)
                {
                    if (c.Name.Equals(line.Trim()))
                    {
                        user.Send("Name already exists!");
                        user.Prompt("Your character's name is ");
                        return;
                    }
                }
            }

            user.UserAccount.CurrentCharacter.Name = line.Trim();
            user.CurrentState.NewCharacter = NewCharacterState.Sex;
            user.Prompt(user.UserAccount.CurrentCharacter.Name + "'s gender is ");
        }

        public static void Sex(String line, User user, List<Race> races)
        {
            if (line.Trim().ToLower().Equals("m") || line.Trim().ToLower().Equals("male"))
            {
                user.UserAccount.CurrentCharacter.Gender = Gender.Male;
                user.CurrentState.NewCharacter = NewCharacterState.Race;
                user.Send(user.UserAccount.CurrentCharacter.Name + " is male.");

                foreach (Race r in races)
                {
                    user.Send("\t" + r.Name);
                }

                user.Prompt("What race will he be? ");
            }
            else if (line.Trim().ToLower().Equals("f") || line.Trim().ToLower().Equals("female"))
            {
                user.UserAccount.CurrentCharacter.Gender = Gender.Female;
                user.CurrentState.NewCharacter = NewCharacterState.Race;
                user.Send(user.UserAccount.CurrentCharacter.Name + " is female.");

                foreach (Race r in races)
                {
                    user.Send("\t" + r.Name);
                }

                user.Prompt("What race will she be? ");
            }
            else if (line.Trim().ToLower().Equals("n") || line.Trim().ToLower().Equals("neutral"))
            {
                user.UserAccount.CurrentCharacter.Gender = Gender.Neutral;
                user.CurrentState.NewCharacter = NewCharacterState.Race;
                user.Send(user.UserAccount.CurrentCharacter.Name + " is neutral.");

                foreach (Race r in races)
                {
                    user.Send("\t" + r.Name);
                }

                user.Prompt("What race will it be? ");
            }
            else
            {
                user.Send("Invalid input");
                user.Prompt(user.UserAccount.CurrentCharacter.Name + "'s gender is ");
            }
        }
    }
}
